﻿using System;
using System.Collections.Generic;
using System.Linq;
using ParserGenerator.Exceptions;
using ParserGenerator.Extensions;
using ParserGenerator.Input;

namespace ParserGenerator.SyntacticAnalysis
{
    partial class ByteStringLiteral
    {
        public bool IsEmpty => Value.Count == 0;
        public bool IsSingle => Value.Count == 1;
        public bool IsMultiple => Value.Count > 1;

        public byte AsSingle
        {
            get
            {
                if (!IsSingle)
                {
                    ValueErrorException e = new ValueErrorException(SR.ByteStringLiteralNotSingle);
                    e.AddLocation(Context);
                    throw e;
                }
                return Value[0];
            }
        }

        public static ByteStringLiteral New(byte[] value, bool? isQuote = null, SourceContext context = null)
        {
            if (value == null)
                throw new ArgumentNullException(nameof(value));
            return New(value.AsReadOnly(), isQuote, context);
        }

        public static ByteStringLiteral New(List<byte> value, bool? isQuote = null, SourceContext context = null)
        {
            if (value == null)
                throw new ArgumentNullException(nameof(value));
            return New(value.ToArray(), isQuote, context);
        }

        public static ByteStringLiteral New(IEnumerable<byte> value, bool? isQuote = null, SourceContext context = null)
        {
            if (value == null)
                throw new ArgumentNullException(nameof(value));
            return New(value.ToArray(), isQuote, context);
        }

        public static ByteStringLiteral Create(params byte[] items)
        {
            if (items == null)
                throw new ArgumentNullException(nameof(items));
            return New(items, null, null);
        }
    }
}
